var knex = require('../dbhelper').instance();
var Promise = require('bluebird');
module.exports = {
    getlist: function (condition) {
        return new Promise(function (resolve, reject) {
            knex.transaction(function (trx) {
                return Promise.all([
                        knex('folder').where('foldername', 'like', '%' + condition.key + '%')
                        .select('*').limit(condition.limit).offset((condition.page - 1) * condition.limit).orderBy('createat', 'desc').transacting(trx),

                        knex('folder').where('foldername', 'like', '%' + condition.key + '%')
                        .select(knex.raw('count(1) as total')).transacting(trx)
                    ])
                    .then(trx.commit)
                    .catch(trx.rollback);
            }).then(function (resp) {
                resolve(resp);
            }).catch(function (err) {
                console.error(err);
            });
        });
    },
    addfolder: function (params) {
        return new Promise(function (resolve, reject) {
            knex('folder').insert(params).then(function (data) {
                resolve(data);
            }).catch(function (error) {
                console.error(error);
                reject(error);
            });
        });
    },
    addresource: function (params) {
        return new Promise(function (resolve, reject) {
            knex('resource').insert(params).then(function (data) {
                resolve(data);
            }).catch(function (error) {
                console.error(error);
                reject(error);
            });
        });
    },
    addresource: function (params) {
        return new Promise(function (resolve, reject) {
            knex('resource').insert(params).then(function (data) {
                resolve(data);
            }).catch(function (error) {
                console.error(error);
                reject(error);
            });
        });
    },
    resourcelist: function (condition) {
        return new Promise(function (resolve, reject) {
            knex.transaction(function (trx) {
                return Promise.all([
                        knex('resource').leftJoin('folder', 'resource.folderid', 'folder.folderid').where('folder.folderid', '=', knex.raw('?', [condition.folderid]))
                        .select('resource.*', 'folder.foldername', 'folder.path').limit(condition.limit).offset((condition.page - 1) * condition.limit).orderBy('createat', 'desc').transacting(trx),

                        knex('resource').leftJoin('folder', 'resource.folderid', 'folder.folderid').where('folder.folderid', '=', knex.raw('?', [condition.folderid]))
                        .select(knex.raw('count(1) as total')).transacting(trx)
                    ])
                    .then(trx.commit)
                    .catch(trx.rollback);
            }).then(function (resp) {
                resolve(resp);
            }).catch(function (err) {
                console.error(err);
            });
        });
    }
};